package com.bci.pwtz.admin.service.impl;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.admin.service.AdminSmsSendLogService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.AdminSmsSendLogMapper;
import com.bci.pwtz.mysql.model.AdminSmsSendLog;
@Service
public class AdminSmsSendLogServiceImpl implements AdminSmsSendLogService
{
    AdminSmsSendLogMapper objMapper;
    private static final Logger logger=Logger.getLogger(AdminSmsSendLogServiceImpl.class);
    
    
    public AdminSmsSendLogMapper getObjMapper()
    {
        return objMapper;
    }

    public void setObjMapper(AdminSmsSendLogMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    @Override
    public int add(AdminSmsSendLog obj) throws PwtzDatabaseException
    {
        int result = 0;
        if(logger.isDebugEnabled()){
            logger.debug("add : obj"+obj.toString());
        }
        if(obj==null){
            if(logger.isDebugEnabled()){
                logger.debug("add,obj is null ");
            }
            return result;
        }
        try{
            result=objMapper.insertSelective(obj);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public PaginationList<AdminSmsSendLog> selectSmsSendLogList(String begintime, String endtime, String operatorname, String content,int pagesize,int pageno) throws PwtzDatabaseException
    {
        PaginationList<AdminSmsSendLog> plist=new PaginationList<AdminSmsSendLog>();
        List<AdminSmsSendLog> list=null;
        if(logger.isDebugEnabled()){
            logger.debug("selectSmsSendLogList : |begintime="+begintime+"|endtime="+endtime+"|operatorname="+operatorname+"|content="+content);
        }
        int beginindex=pagesize*pageno-pagesize;
        try{
            list=objMapper.selectList(begintime, endtime, operatorname, content,beginindex,pagesize);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            logger.debug("selectSmsSendLogList:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pagesize!=0){
            try
            {
                totalCount = objMapper.selectCount(begintime, endtime, operatorname, content);
            }
            catch (DataAccessException e)
            {
                logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pagesize == 0 ? totalCount / pagesize : totalCount / pagesize + 1;
        }else{
            pageno=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        plist.setTotalPage(totalPage);
        plist.setCurrPage(pageno);
        plist.setPageSize(pagesize);
        plist.setRecords(list);
        plist.setTotalRecord(totalCount);
        return plist;
    }

}
